import random
from random import choice, randint, uniform
from sympy import symbols, Eq, solve
from math import ceil, gcd
import math
from fractions import Fraction

"""
输入：年级 、 数量、 难度
输出： 问题序号.问题  ，然后是答案
数量要求： 
    一年级的简单题P1数量不超过 55      P1：一位数不进位加法
    一年级的难度适中题P1数量不超过 55   P1:一位数减法，不出现负数
    一年级的难题数量P1不超过 55        P1:一位数加法，可能出现进位

    二年级的简单题P1数量可以达到 1000    P1:两位不借位减法
    二年级的难度适中题P1数量可以达到 1000  P1:两位数进位加法
    二年级的难题（p1)数量可以达到 1000  但二年级的难题（p2)数量不超过81   P1:两位数借位减法  P2:1-9乘法

    三年级简单P1可达 数量可达 1000   简单P2 数量不超过 81     P1:三位数加法  P2:乘法表内的除法
    三年级中等P1 数量可达 1000   中等P2 数量不超过 600    中等P3 数量不超过 98   P1:三位数减法  P2:三位数整除一位数   P3:两位数除以两位数（整除）
    三年级难题P1 数量可达 1000           P1:最高位为三位数(可以进位），加减混合运算（四项）

    四年级简单P1 数量可达1000        P1:四位数以内加法(可以进位)
    四年级中等P1 数量可达1000  中等P2 数量可达1000     P1:10以内小数加减   P2:两位数乘法
    四年级难题P1 数量可达1000   难题P2 数量可达1000    P1:整数加减乘除三项混合运算  P2:小数点移位

    五年级简单P1 数量可达1000        P1:分数化简   
    五年级中等P1 数量可达1000  中等P2 数量可达1000    P1:分数相加减  P2:小数加减乘两步运算（e.g. 0.3*15+3.57=）
    五年级难题P1 数量可达1000        P1:分数与小数相加减, 答案以分数形式呈现

    六年级简单P1 数量可达1000    P1:分数除整数
    六年级中等P1 数量可达1000  中等P2 数量可达100   中等P3 数量可达1000   P1:分数除以分数   P2:求圆面积或周长 P3：由矩形求圆
    六年级难题P1 数量可达1000  难题P2 数量可达1000  P1:在长方形或正方形内画一个圆， 求圆的最大周长或面积   P2: 鸡兔同笼问题(数量都不超过100只)
"""

"""一年级题目"""


def one_easy_p1(topic):
    """
    一位数不进位加法
    :return: 返回问题和答案
    """
    if topic != 0:
        question, answer = "问题请求错误", "暂无答案"
        return question, answer
    num1 = random.randint(0, 9)
    num2 = random.randint(0, 9 - num1)
    question = f"{num1:^2d} + {num2:^2d} = "
    answer = num1 + num2
    return question, answer


def one_midum_p1(topic):
    """
    一位数减法，不出现负数
    :return: 问题和答案
    """
    if topic != 0:
        question, answer = "问题请求错误", "暂无答案"
        return question, answer
    num1 = random.randint(0, 9)
    num2 = random.randint(0, num1)
    question = f"{num1:^2d} - {num2:^2d} = "
    answer = num1 - num2
    return question, answer


def one_hard_p1(topic):
    """
    一位数加法，可能出现进位
    :return:
    """
    if topic != 0:
        question, answer = "问题请求错误", "暂无答案"
        return question, answer
    num1 = random.randint(0, 9)
    num2 = random.randint(num1, 9)
    question = f"{num1:^2d} + {num2:^2d} = "
    answer = num1 + num2
    return question, answer


"""二年级题目"""


def two_easy_p1(topic):
    """
    两位不借位减法
    """
    if topic != 0:
        question, answer = "问题请求错误", "暂无答案"
        return question, answer
    num1 = random.randint(1, 9)
    num2 = random.randint(0, 9)
    num3 = random.randint(1, num1)
    num4 = random.randint(0, num2)
    n1 = num1 * 10 + num2
    n2 = num3 * 10 + num4
    question = f"{n1:^2d} - {n2:^2d} = "
    answer = n1 - n2
    return question, answer


def two_midum_p1(topic):
    """
    两位数进位加法
    :return:
    """
    if topic!=0:
        question, answer = "问题请求错误", "暂无答案"
        return question, answer
    num1 = random.randint(1, 9)
    num2 = random.randint(0, 9)
    num3 = random.randint(1, 10 - num1)
    num4 = random.randint(num2, 9)
    n1 = num1 * 10 + num2
    n2 = num3 * 10 + num4
    question = f"{n1:^2d} + {n2:^2d} = "
    answer = n1 + n2
    return question, answer


def two_hard(topic):
    """根据topic生成不同的二年级数学难题
    topic = [0, 1, 2]
    """
    question = ""
    answer = 0
    if topic == 0:
        twos = [two_hard_p1, two_hard_p2]
        pro_type = choice(twos)
        question, answer = pro_type()
    elif topic == 1:
        question, answer = two_hard_p1()
    elif topic == 2:
        question, answer = two_hard_p2()
    else:
        question, answer = "问题请求错误", "暂无答案"
    return question, answer

def two_hard_p1():
    """
    两位数借位减法
    :return:
    """
    num1 = random.randint(2, 9)
    num2 = random.randint(0, 9)
    num3 = random.randint(1, num1 - 1)
    num4 = random.randint(num2, 9)
    n1 = num1 * 10 + num2
    n2 = num3 * 10 + num4
    question = f"{n1:^2d} - {n2:^2d} = "
    answer = n1 - n2
    return question, answer


def two_hard_p2():
    """
    1-9乘法
    """
    num1 = random.randint(1, 9)
    num2 = random.randint(1, 9)
    question = f"{num1:^2d} × {num2:^2d} = "
    answer = num1 * num2
    return question, answer


"""三年级"""

def three_easy(topic):
    """根据topic生成不同的三年级简单数学题
    topic = [0, 1, 2]"""
    question = ""
    answer = 0
    if topic == 0:
        twos = [three_easy_p1, three_easy_p2]
        pro_type = choice(twos)
        question, answer = pro_type()
    elif topic == 1:
        question, answer = three_easy_p1()
    elif topic == 2:
        question, answer = three_easy_p2()
    else:
        question, answer = "问题请求错误", "暂无答案"
    return question, answer

def three_easy_p1():
    """
    三位数加法
    :return:
    """
    num1 = random.randint(100, 899)
    num2 = random.randint(100, 999 - num1)
    question = f"{num1:^2d} + {num2:^2d} = "
    answer = num1 + num2
    return question, answer


def three_easy_p2():
    """
    乘法表内的除法
    :return:
    """
    num1 = random.randint(1, 9)
    num2 = random.randint(1, 9)
    num3 = num1 * num2
    question = f"{num3:^2d} ÷ {num2:^2d} = "
    answer = num1
    return question, answer

def three_midum(topic):
    """根据topic生成不同的三年级中等数学题
    topic = [0, 1, 2, 3]"""
    question = ""
    answer = 0
    if topic == 0:
        twos = [three_midum_p1, three_midum_p2, three_midum_p3]
        pro_type = choice(twos)
        question, answer = pro_type()
    elif topic == 1:
        question, answer = three_midum_p1()
    elif topic == 2:
        question, answer = three_midum_p2()
    elif topic == 3:
        question, answer = three_midum_p3()
    else:
        question, answer = "问题请求错误", "暂无答案"
    return question, answer

def three_midum_p1():
    """
    三位数减法
    :return:
    """
    num1 = random.randint(100, 999)
    num2 = random.randint(100, num1)
    question = f"{num1:^2d} - {num2:^2d} = "
    answer = num1 - num2
    return question, answer


def three_midum_p2():
    """
    三位数整除一位数
    :return:
    """
    num1 = random.randint(2, 9)
    num2 = random.randint(12, 99)
    while num1 * num2 < 100:
        num2 = num2 + 4
    num3 = num1 * num2
    question = f"{num3:^2d} ÷ {num1:^2d} = "
    answer = num2
    return question, answer


def three_midum_p3():
    """
    两位数除以两位数（整除）
    :return:
    """
    # 存放2-9的100以内的倍数
    l = [[i for i in range(11, 100 // j + 1)] for j in range(2, 10)]
    l[0].pop(-1)
    l[2].pop(-1)
    l[3].pop(-1)
    num1 = randint(2, 9)
    num2 = choice(l[num1 - 2])
    num3 = num2 * num1
    question = f"{num3:^2d} ÷ {num2:^2d} = "
    answer = num1
    return question, answer


def three_hard_p1(topic):
    """
    最高位为三位数(可以进位），加减混合运算（四项）
    :return:
    """
    if topic != 0:
        question, answer = "问题请求错误", "暂无答案"
        return question, answer
    ops = ['+', '-']
    op1 = choice(ops)
    op2 = choice(ops)
    op3 = choice(ops)
    num1 = randint(100, 999)
    question = ""
    answer = 0
    if op1 == '+' and op2 == '+' and op3 == '-':
        num2 = randint(100, 999)
        num3 = randint(100, 999)
        num4 = randint(100, min(999, num1 + num2 + num3))
        question = f"{num1:^2d} + {num2:^2d} + {num3:^2d} - {num4:^2d} = "
        answer = num1 + num2 + num3 - num4
    elif op1 == '+' and op2 == '-' and op3 == '+':
        num2 = randint(100, 999)
        num4 = randint(100, 999)
        num3 = randint(100, min(999, num1 + num2 + num4))
        question = f"{num1:^2d} + {num2:^2d} - {num3:^2d} + {num4:^2d} = "
        answer = num1 + num2 - num3 + num4
    elif op1 == '-' and op2 == '+' and op3 == '+':
        num3 = randint(100, 999)
        num4 = randint(100, 999)
        num2 = randint(100, min(999, num1 + num3 + num4))
        question = f"{num1:^2d} - {num2:^2d} + {num3:^2d} + {num4:^2d} = "
        answer = num1 - num2 + num3 + num4
    elif op1 == '-' and op2 == '-' and op3 == '+':
        num4 = randint(100, 999)
        num3 = randint(100, min(999, num1 + num4 - 1))
        num2 = randint(1, min(999, num1 + num4 - num3))
        question = f"{num1:^2d} - {num2:^2d} - {num3:^2d} + {num4:^2d} = "
        answer = num1 - num2 - num3 + num4
    elif op1 == '-' and op2 == '+' and op3 == '-':
        num3 = randint(100, 999)
        num4 = randint(100, min(999, num1 + num3 - 1))
        num2 = randint(1, min(999, num1 + num3 - num4))
        question = f"{num1:^2d} - {num2:^2d} + {num3:^2d} - {num4:^2d} = "
        answer = num1 - num2 + num3 - num4
    elif op1 == '+' and op2 == '-' and op3 == '-':
        num2 = randint(100, 999)
        num3 = randint(100, min(999, num1 + num2 - 1))
        num4 = randint(1, min(999, num1 + num2 - num3))
        question = f"{num1:^2d} + {num2:^2d} - {num3:^2d} - {num4:^2d} = "
        answer = num1 + num2 - num3 - num4
    elif op1 == '-' and op2 == '-' and op3 == '-':
        num2 = randint(1, num1 // 2)
        num3 = randint(1, (num1 - num2) // 2)
        num4 = randint(1, num1 - num2 - num3)
        question = f"{num1:^2d} + {num2:^2d} - {num3:^2d} - {num4:^2d} = "
        answer = num1 - num2 - num3 - num4
    else:  # +++
        num2 = randint(100, 999)
        num3 = randint(100, 999)
        num4 = randint(100, 999)
        question = f"{num1:^2d} + {num2:^2d} + {num3:^2d} + {num4:^2d} = "
        answer = num1 + num2 + num3 + num4

    return question, answer


"""四年级"""


def four_easy_p1(topic):
    """
    四位数以内加法(可以进位)
    :return:
    """
    if topic != 0:
        question, answer = "问题请求错误", "暂无答案"
        return question, answer
    num1 = randint(1000, 9999)
    num2 = randint(1000, 9999)
    question = f"{num1:^2d} + {num2:^2d} = "
    answer = num1 + num2
    return question, answer

def four_midum(topic):
    """根据topic生成不同的四年级中等数学题
    topic = [0, 1, 2]"""
    question = ""
    answer = 0
    if topic == 0:
        twos = [four_midum_p1, four_midum_p2]
        pro_type = choice(twos)
        question, answer = pro_type()
    elif topic == 1:
        question, answer = four_midum_p1()
    elif topic == 2:
        question, answer = four_midum_p2()
    else:
        question, answer = "问题请求错误", "暂无答案"
    return question, answer

def four_midum_p1():
    """
    10以内小数加减
    :return:
    """
    ops = ['+', '-']
    operator = choice(ops)
    ws = [1, 2]  # 保存一位小数或两位小数
    w = choice(ws)
    num1 = round(uniform(0.1, 9.9), w)
    if operator == '-':
        num2 = round(uniform(0.1, num1), 2)
        question = f"{num1:.2f} - {num2:.2f} = "
        answer = round(num1 - num2, 2)
    else:
        num2 = round(uniform(0.1, 10 - num1), w)
        question = f"{num1:.2f} + {num2:.2f} = "
        answer = round(num1 + num2, 2)

    return question, answer


def four_midum_p2():
    """
    两位数乘法
    :return:
    """
    num1 = randint(10, 99)
    num2 = randint(10, 99)
    question = f"{num1:^2d} × {num2:^2d} = "
    answer = num1 * num2
    return question, answer

def four_hard(topic):
    """根据topic生成不同的四年级困难数学题
    topic = [0, 1, 2]"""
    question = ""
    answer = 0
    if topic == 0:
        twos = [four_hard_p1, four_hard_p1]
        pro_type = choice(twos)
        question, answer = pro_type()
    elif topic == 1:
        question, answer = four_hard_p1()
    elif topic == 2:
        question, answer = four_hard_p2()
    else:
        question, answer = "问题请求错误", "暂无答案"
    return question, answer

def four_hard_p1():
    """
    整数加减乘除三项混合运算
    :return:
    """
    ops = ['+', '-', '*', '//']
    operator = [choice(ops) for i in range(2)]
    num1 = randint(100, 999)
    if operator[0] == '+' and operator[1] == '+':
        num2 = randint(100, 999)
        num3 = randint(100, 999)
    elif operator[0] == '+' and operator[1] == '-':
        num2 = randint(100, 999)
        num3 = randint(100, min(999, num1 + num2))
    elif operator[0] == '+' and operator[1] == '*':
        num2 = randint(100, 999)
        num3 = randint(1, 20)
    elif operator[0] == '+' and operator[1] == '//':
        num3 = randint(2, 99)
        num4 = randint(2, 1000 // num3)
        num2 = num3 * num4
    elif operator[0] == '-' and operator[1] == '+':
        num3 = randint(100, 999)
        num2 = randint(100, min(999, num1 + num3))
    elif operator[0] == '-' and operator[1] == '-':
        num2 = randint(49, num1 // 2)
        num3 = randint(49, num1 // 2)
    elif operator[0] == '-' and operator[1] == '*':
        num2 = randint(2, 50)
        num3 = randint(2, num1 // num2)
    elif operator[0] == '-' and operator[1] == '//':
        num2 = randint(100, 999)
        fac = find_factors(num2)
        available = []
        for i in range(len(fac)):
            if num2 // fac[i] < num1:
                available.append(fac[i])
        num3 = choice(available)
    elif operator[0] == '*' and operator[1] == '+':
        num2 = randint(10, 49)
        num3 = randint(100, 999)
    elif operator[0] == '*' and operator[1] == '-':
        num3 = randint(100, 999)
        num2 = randint(num3 // num1, 100)
    elif operator[0] == '*' and operator[1] == '*':
        num2 = randint(10, 20)
        num3 = randint(10, 20)
    elif operator[0] == '*' and operator[1] == '//':
        num2 = randint(10, 20)
        fac = find_factors(num2 * num1)
        num3 = choice(fac)
    elif operator[0] == '//' and operator[1] == '+':
        fac = find_factors(num1)
        num2 = choice(fac)
        num3 = randint(100, 999)
    elif operator[0] == '//' and operator[1] == '-':
        fac = find_factors(num1)
        num2 = choice(fac)
        num3 = randint(1, num1 // num2)
    elif operator[0] == '//' and operator[1] == '*':
        fac = find_factors(num1)
        num2 = choice(fac)
        num3 = randint(10, 99)
    elif operator[0] == '//' and operator[1] == '//':
        fac_1 = find_factors(num1)
        num2 = choice(fac_1)
        fac_2 = find_factors(num1 // num2)
        num3 = choice(fac_2)
    str = f"{num1} {operator[0]} {num2} {operator[1]} {num3}"
    answer = eval(str)
    for i, op in enumerate(operator):
        if op == '*':
            operator[i] = '×'
        elif op == '//':
            operator[i] = '÷'
    question = f"{num1:^2d} {operator[0]} {num2:^2d} {operator[1]} {num3:^2d} = "

    return question, answer


def find_factors(n):
    """
    寻找n的因数列表
    :param n:
    :return:
    """
    factors = []
    for i in range(1, int(math.sqrt(n)) + 1):
        if n % i == 0:
            factors.append(i)
            factors.append(n // i)
    return factors


def four_hard_p2():
    """
    小数点移位
    :return:
    """
    ops = ['×', '÷']
    operator = choice(ops)
    question = ""
    answer = 0
    if operator == '×':
        chengs = [0.001, 0.01, 0.1, 10, 100, 1000]
        cheng = choice(chengs)
        if cheng < 1:
            num1 = randint(1, 999)
            question = f"{num1:^2d} × {cheng} = "
            answer = round(num1 * cheng, 3)
        else:
            num1 = round(uniform(0.01, 9.99), 3)
            question = f"{num1:.3f} × {cheng} = "
            answer = round(num1 * cheng, 3)
            answer = int(answer) if answer == ceil(answer) else answer
    else:
        chus = [10, 100, 1000]
        chu = choice(chus)
        if chu == 1000:
            num1 = randint(1, 999)
            question = f"{num1:^2d} ÷ {chu} = "
            answer = round(num1 / chu, 3)
        elif chu == 100:
            num1 = round(uniform(1, 999), 1)
            question = f"{num1} ÷ {chu} = "
            answer = round(num1 / chu, 3)
        else:
            num1 = round(uniform(1, 999), 2)
            question = f"{num1} ÷ {chu} = "
            answer = round(num1 / chu, 3)

    return question, answer


"""五年级"""


def get_100():
    """获得100以内的质数，除了1和它本身还有其他因数"""
    ex = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
    ds_1 = [i for i in range(2, 101)]
    ds = []
    for i in range(len(ds_1)):
        if ds_1[i] not in ex:
            ds.append(ds_1[i])
    return ds


def find_numerator(n):
    """寻找分母为n的，可化简的分子,分子小于分母"""
    fac = find_factors(n)
    fac.remove(1)
    fac.remove(n)
    numerator = []
    for i in range(2, n):
        if gcd(i, n) in fac:
            numerator.append(i)
    return numerator


def find_gcd_1(n, top):
    """
    寻找 top以内 与n互质的数
    :param n: n>=2
    :return:
    """
    gcd_1 = []
    for i in range(2, top):
        if gcd(i, n) == 1:
            gcd_1.append(i)
    return gcd_1


def find_gcd_2(n, top):
    """
    寻找 top以内 与n 最大公因数大于1且不为n的数
    :param n: n>=2
    :return:
    """
    gcd_2 = []
    for i in range(2, top):
        if gcd(i, n) != 1 and i != n:
            gcd_2.append(i)
    return gcd_2


def five_easy_p1(topic):
    """
    分数化简
    :return:
    """
    if topic != 0:
        question, answer = "问题请求错误", "暂无答案"
        return question, answer
    ds = get_100()
    num1 = choice(ds)
    nume = find_numerator(num1)
    num2 = choice(nume)
    question = f"{num2} / {num1} = "
    answer = Fraction(num2, num1)
    return question, answer

def five_midum(topic):
    """根据topic生成不同的五年级中等数学题
    topic = [0, 1, 2]"""
    question = ""
    answer = 0
    if topic == 0:
        twos = [five_midum_p1, five_midum_p1]
        pro_type = choice(twos)
        question, answer = pro_type()
    elif topic == 1:
        question, answer = five_midum_p1()
    elif topic == 2:
        question, answer = five_midum_p2()
    else:
        question, answer = "问题请求错误", "暂无答案"
    return question, answer

def five_midum_p1():
    """
    分数相加减（分母相同|分母之间的最大公因数为1）
    :return:
    """
    choices = [0, 1, 2]  # 0代表分母相同的分数相加， 1代表分母之间最大公因数为1的分数相加， 2代表分母之间最大公因数大于1且不相等的两个分数相加
    ch = choice(choices)
    d1 = d2 = 1
    n1 = n2 = 1
    if ch == 0:
        """分母相同的分数相加"""
        d1 = randint(2, 100)
        n1 = randint(1, d1)
        d2 = d1
        n2 = randint(1, d2)
    elif ch == 1:
        """分母之间最大公因数为1的分数相加"""
        d1 = randint(2, 100)
        gcd_1 = find_gcd_1(d1, 100)
        d2 = choice(gcd_1)
        n1 = randint(1, d1)
        n2 = randint(1, d2)
    else:
        """分母之间最大公因数大于1且不相等的两个分数相加"""
        ds = get_100()
        d1 = choice(ds)  # 应该从100以内的质数中选取
        gcd_2 = find_gcd_2(d1, 100)
        d2 = choice(gcd_2)
        n1 = randint(2, d1)
        n2 = randint(2, d2)
    question = f"{n1}/{d1} + {n2}/{d2} = "
    answer = Fraction(n1, d1) + Fraction(n2, d2)

    return question, answer


def five_midum_p2():
    """
    小数加减乘两步运算（e.g. 0.3*15+3.57=）
    :return:
    """
    ops = ['+', '-']
    operator = choice(ops)
    num1 = round(uniform(0.1, 5), 2)
    num2 = randint(3, 49)
    num3 = 0
    answer = 0
    if operator == '+':
        num3 = round(uniform(0.1, 19.9), 2)
        answer = round(num1 * num2 + num3, 2)
    else:
        num3 = round(uniform(0.1, min(19.9, num1 * num2)), 2)
        answer = round(num1 * num2 - num3, 2)
    question = f"{num1} * {num2} {operator} {num3} = "
    return question, answer


def five_hard_p1(topic):
    """
    分数与小数相加减, 答案以分数形式呈现
    :return:
    """
    if topic != 0:
        question, answer = "问题请求错误", "暂无答案"
        return question, answer

    ops = ['-', '+']
    operator = choice(ops)
    question = ""
    answer = 0
    choices = [0, 1]  # 0表示分数是最简分数， 1表示分数不是最简分数
    ch = choice(choices)
    if operator == '+':
        num1 = round(uniform(0.1, 19.9), 2)
        if ch == 0:
            d2 = randint(10, 99)
            gcd_1 = find_gcd_1(d2, 100)
            n2 = choice(gcd_1)
        else:
            ds = get_100()
            d2 = choice(ds)
            gcd_2 = find_gcd_2(d2, 100)
            n2 = choice(gcd_2)
        question = f"{num1} + {n2}/{d2} = "
        answer = Fraction(int(num1 * 100), 100) + Fraction(n2, d2)
    else:  # '-'
        d1 = randint(10, 99)
        if ch == 0:
            gcd_1 = find_gcd_1(d1, 500)
            n1 = choice(gcd_1)
            top = round(float(Fraction(n1, d1)), 2) - 0.01
            num2 = round(uniform(0.01, top), 2)
        else:
            gcd_2 = find_gcd_2(d1, 500)
            n1 = choice(gcd_2)
            top = round(float(Fraction(n1, d1)), 2) - 0.01  # 防止四舍五入的情况  例如将 302/58 = 5.206 -》 5.21
            num2 = round(uniform(0.01, top), 2)  # 将起始设置为0.01 ,是防止极端情况 2/99 = 0.02
        question = f"{n1}/{d1} - {num2} = "
        answer = Fraction(n1, d1) - Fraction(int(num2 * 100), 100)
    return question, answer


"""六年级"""


def six_easy_p1(topic):
    """分数除整数"""
    if topic != 0:
        question, answer = "问题请求错误", "暂无答案"
        return question, answer
    num1 = round(uniform(0.1, 9.9), 2)
    num2 = randint(3, 49)
    question = f"{num1} * {num2}  = "
    answer = round(num1 * num2, 2)
    return question, answer

def six_midum(topic):
    """根据topic生成不同的六年级中等数学题
    topic = [0, 1, 2, 3]"""
    question = ""
    answer = 0
    if topic == 0:
        twos = [six_midum_p1, six_midum_p2, six_midum_p3]
        pro_type = choice(twos)
        question, answer = pro_type()
    elif topic == 1:
        question, answer = six_midum_p1()
    elif topic == 2:
        question, answer = six_midum_p2()
    elif topic == 3:
        question, answer = six_midum_p3()

    return question, answer

def six_midum_p1():
    """
    分数除以分数
    :return:
    """
    choices = [0, 1, 2]  # 0表示 两个分数的分子分母都是随机生成的  1 表示 第一个分数的分子和第二个分数的分子相同  2 表示 第一个分数的分子和第二个分数的分子的最大公因数大于1

    ch = choice(choices)
    n1 = d1 = n2 = d2 = 0
    if ch == 0:
        n1 = randint(2, 99)
        d1 = randint(2, 99)
        n2 = randint(2, 99)
        d2 = randint(2, 99)
    elif ch == 1:
        n1 = randint(2, 99)
        d1 = randint(2, 99)
        n2 = n1
        d2 = randint(2, 99)
    else:  # ch == 2
        n1 = randint(2, 49)
        d1 = randint(2, 99)
        gcd_2 = find_gcd_2(n1, 100)
        n2 = choice(gcd_2)
        d2 = randint(2, 99)

    question = f"{n1}/{d1} ÷ {n2}/{d2}  = "
    answer = Fraction(n1, d1) / Fraction(n2, d2)
    return question, answer


def six_midum_p2():
    """
    求圆面积或周长
    :return:
    """
    choices = [0, 1, 2]  # 0表示圆有直径求面积  ； 1表示圆有半径求周长 ；2表示圆有周长求面积 ; 3表示长方形有长和宽，求面积 ; 4 表示长方形有长和宽，求周长;
    # 5表示正方形有周长，求边 ; 6 表示正方形有周长，求面积
    ch = choice(choices)
    PI = 3.14
    question = ""
    answer = 0
    if ch == 0:
        r = randint(2, 50)
        d = 2 * r
        question = f"一个圆的直径是{d},面积是( )。"
        answer = round(r * r * PI, 2)
    elif ch == 1:
        r = randint(2, 50)
        question = f"一个圆的半径是{r}, 周长是( )。"
        answer = round(2 * r * PI, 2)
    elif ch == 2:
        r = randint(2, 30)
        c = round(2 * r * PI, 2)
        question = f"一个圆的周长为{c}, 面积是( )。"
        answer = round(r * r * PI, 2)

    return question, answer

def six_midum_p3():
    """由矩形求圆"""
    choices = [3, 4, 5, 6]
    # 3表示长方形有长和宽，求面积;
    # 4表示长方形有长和宽，求周长;
    # 5表示正方形有周长，求边 ;
    # 6 表示正方形有周长，求面积
    ch = choice(choices)
    PI = 3.14
    question = ""
    answer = 0
    if ch == 3:
        chang = randint(10, 99)
        kuan = randint(10, 99)
        question = f"一个长方形,长为{chang},宽为{kuan},面积是( )。"
        answer = chang * kuan
    elif ch == 4:
        chang = randint(11, 99)
        kuan = randint(10, chang - 1)
        question = f"一个长方形,长为{chang},宽为{kuan},周长是( )。"
        answer = (chang + kuan) * 2
    elif ch == 5:
        bian = randint(10, 99)
        c = bian * 4
        question = f"正方形周长为{c}, 边长是( )。"
        answer = bian
    elif ch == 6:
        bian = randint(10, 99)
        c = bian * 4
        question = f"正方形周长为{c}, 面积是( )。"
        answer = bian * bian
    return question, answer

def six_hard(topic):
    """根据topic生成不同的六年级困难数学题
    topic = [0, 1, 2]"""
    question = ""
    answer = 0
    if topic == 0:
        twos = [six_hard_p1, six_hard_p2]
        pro_type = choice(twos)
        question, answer = pro_type()
    elif topic == 1:
        question, answer = six_hard_p1()
    elif topic == 2:
        question, answer = six_hard_p2()
    else:
        question, answer = "问题请求错误", "暂无答案"
    return question, answer

def six_hard_p1():
    """
    在长方形或正方形内画一个圆， 求圆的最大周长或面积， 求矩形内的最大圆
    :return:
    """
    shapes = [0, 1]  # 0代表长方形;1代表正方形
    shape = choice(shapes)
    question = ""
    answer = 0
    if shape == 0:
        choices = [0, 1]  # 0代表求周长;1代表求面积
        ch = choice(choices)
        if ch == 0:
            chang = randint(11, 99)
            kuan = randint(10, chang - 1)
            c = (chang + kuan) * 2
            question = f"长方形长为{chang},周长为{c},则在长方形内画一个圆，圆的最大周长是( )。"
            answer = round(kuan * 3.14, 2)
        else:
            chang = randint(11, 99)
            num1 = randint(5, (chang - 1) // 2)
            kuan = num1 * 2
            question = f"长方形长为{chang},宽为{kuan},则在长方形内画一个圆，圆的最大面积是( )。"
            answer = round(num1 * num1 * 3.14, 2)
    else:  # shape = 1
        choices = [0, 1]  # 0代表求周长;1代表求面积
        ch = choice(choices)
        if ch == 0:
            bian = randint(10, 99)
            c = bian * 4
            question = f"正方形周长为{c},则在正方形内画一个圆,圆的最大周长是( )。"
            answer = round(bian * 3.14, 2)
        else:
            num1 = randint(5, 49)
            bian = num1 * 2
            question = f"正方形边长为{bian},则在正方形内画一个圆,圆的最大面积是( )。"
            answer = round(num1 * num1 * 3.14, 2)
    return question, answer


def six_hard_p2():
    """
    鸡兔同笼问题(数量都不超过100只)
    :return:
    """
    chicken = randint(10, 99)
    rabbit = randint(10, 99)
    feet = chicken * 2 + rabbit * 4
    head = chicken + rabbit
    choices = [0, 1]  # 0代表问鸡有多少只; 1代表问兔子有多少只
    ch = choice(choices)
    question = ""
    answer = 0
    if ch == 0:
        question = f"鸡兔同笼共{head}头,{feet}条腿。问鸡的个数为( )。"
        answer = chicken
    else:
        question = f"鸡兔同笼共{head}头,{feet}条腿。问兔子的个数为( )。"
        answer = rabbit
    return question, answer


def generate_math_problems(grades, topic, num_problems, difficulty):
    """生成指定年级的指定数量和难度的题目"""
    problems = []
    if difficulty == 'easy':
        problem_type = choice(grades[0])
    elif difficulty == 'midum':
        problem_type = choice(grades[1])
    else:
        problem_type = choice(grades[2])

    for i in range(num_problems):
        question, answer = problem_type(topic)
        if question == "问题请求错误" and answer == "暂无答案":
            problems.append((question, answer))
            break
        while (question, answer) in problems:
            # 防止出现重复的题目
            question, answer = problem_type(topic)
        problems.append((question, answer))

    # questions = {'problems': [], 'answers': []}
    questions = []
    for i, (problem, answer) in enumerate(problems, 1):
        # print(f"{i}. {problem}")
        # questions['problems'].append(f"{i}. {problem}")
        questions.append({'question_text': f"{problem}", 'answer': f"{answer}"})
    # for i, (problem, answer) in enumerate(problems, 1):
    #     # print(f"{i}. {answer}")
    #     if (answer < 0):
    #         print("*" * 50)
    #     questions['answers'].append(f"{i}. {answer}")
    return questions


Grades = ['一年级', '二年级', '三年级', '四年级', '五年级', '六年级']
one = [[one_easy_p1], [one_midum_p1], [one_hard_p1]]
two = [[two_easy_p1], [two_midum_p1], [two_hard]]
three = [[three_easy], [three_midum], [three_hard_p1]]
four = [[four_easy_p1], [four_midum], [four_hard]]
five = [[five_easy_p1], [five_midum], [five_hard_p1]]
six = [[six_easy_p1], [six_midum], [six_hard]]
grades = [one, two, three, four, five, six]


def get_request(grade, topic, number, difficulty):
    index = Grades.index(grade)
    questions = generate_math_problems(grades[index], topic, number, difficulty)
    return questions


if __name__ == '__main__':
    questions = get_request('六年级', 3, 3, 'hard')
    for q in questions:
        print(q)
    # Grades = ['一年级', '二年级', '三年级', '四年级', '五年级', '六年级']
    # for grade in Grades:
    #     questions = get_request(grade, int('3'), 10, 'hard')
    #     for q in questions:
    #         print(q)
    # for problem in questions['problems']:
    #     print(problem)
    # for answer in questions['answers']:
    #     print(answer)
# problems = []
# func = "six_hard_p2"
# for i in range(1000):
#     question, answer = eval(func)()
#     while (question, answer) in problems:
#         # 防止出现重复的题目
#         question, answer = eval(func)()
#     problems.append((question, answer))
#
# questions = {'problems': [], 'answers': []}
# for i, (problem, answer) in enumerate(problems, 1):
#     print(f"{i}. {problem}")
#     questions['problems'].append(f"{i}. {problem}")
#
# for i, (problem, answer) in enumerate(problems, 1):
#     print(f"{i}. {answer}")
#     if(answer<0):
#         print("*"*50)
#     questions['answers'].append(f"{i}. {answer}")
#

# print(questions)